package cn.tools.dozer.config;

import com.github.dozermapper.core.Mapper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: e
 * @date: 2021/12/7
 * @description:
 */

public class DozerUtils {
    private Mapper mapper;

    public DozerUtils(Mapper mapper){
        this.mapper = mapper;
    }

    public Mapper getMapper(){
        return this.mapper;
    }

    //封装方法，判定一下是否为空
    public <T> T map(Object source,Class<T> destinationClass){
        if(source == null){
            return null;
        }
        return mapper.map(source,destinationClass);
    }


    /*
    mapId主要用于处理两个类之间拥有不同名称的值想要进行转换值，可以创建一个dozer.xml文件在yml中配置指向位置，然后参考下列配置
    yml配置可以参照
        dozer:
          mappingFiles:
            - classpath:dozer/global.dozer.xml


        配置在mappings中
        <mapping date-format="yyyy-MM-dd" map-id="user">
            <class-a>com.entity.UserEntity</class-a>
            <class-b>com.dto.UserDTO</class-b>
            <!--使用field标签，设定出classA与ClassB名称不同但是要进行转换的值-->
            <field>
                <a>id</a>
                <b>userId</b>
            </field>
        </mapping>
     */
    public <T> T map(Object source,Class<T> destinationClass,String mapId){
        if(source == null){
            return null;
        }
        return mapper.map(source,destinationClass,mapId);
    }


    public void map(Object source,Object destination){
        if(source ==null){
            return ;
        }
        mapper.map(source,destination);
    }

    public void map(Object source,Object destination,String mapId){
        if(source ==null){
            return ;
        }
        mapper.map(source,destination,mapId);
    }


    /**
     * @author: keno
     * @date:   2021/12/7
     * @description: 集合处理类
     */
    public <T,E> List<T> mapList(Collection<E> sourceList,Class<T> destinationClass){
        return mapPage(sourceList,destinationClass);
    }


    /**
     * @author: keno
     * @date:   2021/12/7
     * @description: list集合转换
     */
    public <T,E> List<T> mapPage(Collection<E> sourceList,Class<T> destinationClass) {
        if(sourceList == null || sourceList.isEmpty() || destinationClass == null){
            return Collections.emptyList();
        }

        List<T> destinationList = sourceList.stream()
                .filter(item -> item != null)
                .map((sourceObject) -> mapper.map(sourceObject, destinationClass))
                .collect(Collectors.toList());

        return destinationList;
    }

    /**
     * @author: keno
     * @date:   2021/12/7
     * @description: set集合转换
     */

    public <T,E> Set<T> mapSet(Collection<E> sourceList, Class<T> destinationClass) {
        if(sourceList == null || sourceList.isEmpty() || destinationClass == null){
            return Collections.emptySet();
        }

        Set<T> destinationList = sourceList.stream()
                .map((sourceObject) -> mapper.map(sourceObject, destinationClass))
                .collect(Collectors.toSet());

        return destinationList;
    }

}
